/**
 * 
 */
package org.softTank.BC.manager;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

import org.apache.log4j.Logger;
import org.softTank.BC.manager.util.CategoryParser;
import org.softTank.BC.rs.bean.CategoryBean;
import org.softTank.BC.rs.bean.TypeBean;

/**
 * This class managers ways of getting category information. If necessary, edit
 * this class to add ways of getting data.
 * 
 * @author Yuzone
 * 
 */
public class CategoryManager {

	private CategoryParser categoryParser;
	protected Logger logger;

	public CategoryManager() {
		logger = Logger.getLogger(this.getClass());
	}
	public List<TypeBean> getTypes() {
		if (categoryParser == null) {
			try {
				String fileAddress = this.getClass().getClassLoader()
						.getResources("categoryList.xml").nextElement()
						.getPath();
				URLDecoder.decode(fileAddress, "GBK");
				File listFile = new File(fileAddress);
				logger.debug("Get the file at: " + listFile.toString());
				categoryParser = new CategoryParser(listFile);
			} catch (IOException e) {
				logger.error("Cannot find categoryList.xml! " + e.getMessage());
			}
		}
		List<TypeBean> types = categoryParser.getTypes();
		for (TypeBean tb : types) {
			if (tb.getCategory() == null) {
				break;
			}
			tb.setIsBranch("true");
			for (CategoryBean cb : tb.getCategory()) {
				cb.setIsBranch("true");
			}
		}

		return types;
	}

	public CategoryParser getCategoryParser() {
		return categoryParser;
	}

	public void setCategoryParser(CategoryParser categoryParser) {
		this.categoryParser = categoryParser;
	}
}
